package org.jeecg.modules.demo.moenydata.uitils;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author yw
 * @Date 2021/3/9 10:04
 */
@Slf4j
public  class TimeUtil {

    /*
     * 将时间转换为时间戳
     */

    public static String dateToStamp(String s) throws ParseException {
        String res="";
        String dateReg="yyyy-MM-dd HH:mm:ss";
        if(s!=null&&!s.equals("")&&!s.equals("0001-01-01T00:00:00Z")){
            if(s.contains("T")){
                if(s.contains(".")){
                    String[] str=s.split("T");
                    String txt2=s.substring(s.indexOf("T")+1,s.indexOf("."));
                    s=str[0]+" "+txt2;
                }else{
                    return res;
                }
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateReg);
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime()/1000;
            res = String.valueOf(ts);
        }

        return res;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res=null;
        if (s!=null&&!s.equals("")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long lt = new Long(s);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
        }

        return res;
    }
    /*
     * 获取当前日期时间戳
     */
    public static String getNowTimestamp(){
        long time=System.currentTimeMillis()/1000;
        return String.valueOf(time);
    }
    /*
     * 获取当前日期 YYYY-MM-DD
     */
    public static String getNowTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String time=formatter.format(new Date());
        return time;
    }
    /*
     * 获取当前日期 YYYYMMDD
     */
    public static String getNowTime2(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String time=formatter.format(new Date());
        return time;
    }
    /*
     * 原格式yyyyMMdd
     * param type 加上- 取yyyy-MM-dd格式
     *
     */
    public static String getPreTime(String type){
        String tp="yyyyMMdd";
        if(type.equals("-")){
            tp="yyyy-MM-dd";
        }
        Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tp);
        String yesterday = simpleDateFormat.format(today);//获取昨天日期

        return yesterday;
    }

    public static String getNowDate(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=formatter.format(new Date());
        return time;
    }
    
    /**
     * 获取天数（两时间戳的差值）
     */
    public static Double getStampDIFF(String stamp1,String stamp2){
        long time1=Long.valueOf(stamp1);
        long time2=Long.valueOf(stamp2);
        long timeDiff=0;
        long timeHour=0;
        //最后登录时间小于注册时间返回0
        if(time2<time1){
            log.info("小于注册时间");
            return 0.0;
        }
        timeDiff=time2-time1;
        timeHour = (timeDiff / (60 * 60 ) );
        Double day=timeHour/24.0;
        return day;
    }

    /** zrikka start */
    /**
     * 获取几天前的日期
     * @return
     */
    public static String getBeforeDate(int num){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -num); //得到前一天
        Date date = calendar.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(date);
        return format;
    }
}
